package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.data.Result;
import com.dk.data.ResultEnum;
import com.dk.service.es.EsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("es")
public class EsController {

    @Reference
    private EsService esService;

    @RequestMapping("toSearch")
    public String toSearch(String title, Model model) {
        if (null ==title || "".equals(title)) {
            return "shop";
        }
        model.addAttribute("title",title);
        return "search";
    }

    @RequestMapping("getItem")
    @ResponseBody
    public Result getItem(@RequestParam(defaultValue = "1") Integer pageNumber,
                          @RequestParam(defaultValue = "10") Integer pageSize, String title) {
        try {
            String item = esService.getItem(pageNumber, pageSize, title);
            return Result.response(ResultEnum.OK).setData(item);
        } catch (Exception e) {
            return Result.response(ResultEnum.FAIL);
        }
    }

}
